﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilsBase;
using UtilsORM.MySqlData;
using UtilsORM;
using UtilsORM.MySqlData;
using UtilsCache;

namespace GatewayServerService.Data
{
    public class DBIotSystemLogError
    {
        public DBIotSystemLogError()
        {

        }

        static string LogSave = UtilAppsettingCache.getSetting("base:logSave");
        public void addSystemLogError(string dmsg, string remark)
        {
            EFYihongIotContext ef = new EFYihongIotContext();

            //EFYihongIotContext efdc = new EFYihongIotContext();
            try
            {
                if (LogSave == "false")
                {
                    return;
                }

                iot_system_error_log adderror = new iot_system_error_log();
                adderror.l_guid = Guid.NewGuid().ToString();
                adderror.l_msg = dmsg;
                adderror.l_time = DateTime.Now;
                adderror.l_remark = remark;
                adderror.l_type = "error";
                ef.iot_system_error_log.Add(adderror);
                ef.SaveChanges();

                //dc_iot_system_error_log dclog = new UtilsClassClone().CloneByReflection<iot_system_error_log, dc_iot_system_error_log>(adderror);
                //efdc.dc_iot_system_error_log.AddAsync(dclog);
                //efdc.SaveChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

        }

        public void addSystemLogError(string dguid, string dmsg, string remark)
        {
            EFYihongIotContext ef = new EFYihongIotContext();

            try
            {
                if (LogSave == "false")
                {
                    return;
                }
                iot_system_error_log adderror = new iot_system_error_log();
                adderror.l_d_guid = dguid;
                adderror.l_guid = DateTime.Now.ToString();
                adderror.l_msg = dmsg;
                adderror.l_time = DateTime.Now;
                adderror.l_remark = remark;
                adderror.l_type = "error";
                ef.iot_system_error_log.AddAsync(adderror);
                ef.SaveChanges();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

        }


        public void addSystemLogErrorForCommand_haddleing(string dguid, string key, string dmsg, string remark)
        {
            EFYihongIotContext ef = new EFYihongIotContext();

            try
            {
                iot_system_error_log error = ef.iot_system_error_log.Where(c => c.l_d_guid == dguid && c.l_key == key).OrderByDescending(c => c.l_id).FirstOrDefault();
                if (error != null)
                {
                    if (error.l_msg == dmsg)
                    {
                        return;
                    }
                    if (error.l_msg != null && dmsg != null && error.l_msg.Length == dmsg.Length)
                    {
                        return;
                    }

                }



            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        public void delSystemLog()
        {
            EFYihongIotContext ef = new EFYihongIotContext();

            try
            {
                //自动删除24小时前数据
                // ef.iot_system_error_log.Where(c => c.l_time < System.DateTime.Now.AddHours(-10)).ExecuteDeleteAsync();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}
